package pl.edu.pk.mech.kkbus.service;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.EntityManagerFactory;

import org.springframework.stereotype.Service;

import pl.edu.pk.mech.kkbus.component.MyEntityManagerFactory;
import pl.edu.pk.mech.kkbus.entities.WorkPlan;

@Service
public class WorkPlanService {
    
    EntityManagerFactory emf = MyEntityManagerFactory.get();
    
    public List<pl.edu.pk.mech.kkbus.dto.WorkPlan> getAllWorkPlan() {
        List<pl.edu.pk.mech.kkbus.dto.WorkPlan> result = new LinkedList<pl.edu.pk.mech.kkbus.dto.WorkPlan>();
        EntityManager em = null;
        
        try {
            em = emf.createEntityManager();
            Query query = em.createQuery("SELECT wp FROM WorkPlan wp");
            List<WorkPlan> results = (List<WorkPlan>) query.getResultList();
            
            Iterator<WorkPlan> iterator = results.iterator();
            while (iterator.hasNext()) {                
                WorkPlan wp = iterator.next();
                
                result.add(new pl.edu.pk.mech.kkbus.dto.WorkPlan(wp.getKey().getId(),
                        wp.getStartHour(), wp.getEndHour(), wp.getDay(), wp.getUserId()));
            }
            
            return result;         
        } catch(NoResultException e) {
            return null;
        } finally {
            if (em != null)
                em.close();
        }
    }
 
    public List<pl.edu.pk.mech.kkbus.dto.WorkPlan> getWorkPlanForUser(long id) {
        List<pl.edu.pk.mech.kkbus.dto.WorkPlan> result = new LinkedList<pl.edu.pk.mech.kkbus.dto.WorkPlan>();
        EntityManager em = null;
        
        try {
            em = emf.createEntityManager();
            Query query = em.createQuery("SELECT wp FROM WorkPlan wp WHERE wp.userId = :userId");
            query.setParameter("userId", id);
            List<WorkPlan> results = (List<WorkPlan>) query.getResultList();
            
            Iterator<WorkPlan> iterator = results.iterator();
            while (iterator.hasNext()) {                
                WorkPlan wp = iterator.next();
                
                result.add(new pl.edu.pk.mech.kkbus.dto.WorkPlan(wp.getKey().getId(), wp.getStartHour(),
                        wp.getEndHour(), wp.getDay(), id));
            }
            
            return result;         
        } catch(NoResultException e) {
            return null;
        } finally {
            if (em != null)
                em.close();
        }
    }
    
    public boolean create(pl.edu.pk.mech.kkbus.dto.WorkPlan wp) {
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            
            WorkPlan workPlan = new WorkPlan(wp.getStartHour(), wp.getEndHour(),
                    wp.getDay(), wp.getUserId());
            em.persist(workPlan);
            
            return true;
        } catch(Exception e) {
            return false;
        } finally {
            if (em != null)
                em.close();
        }
    }
    
    public boolean update(long id, pl.edu.pk.mech.kkbus.dto.WorkPlan wp) {
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            WorkPlan workPlan = em.find(WorkPlan.class, new Long(id));
            workPlan.setDay(wp.getDay());
            workPlan.setEndHour(wp.getEndHour());
            workPlan.setStartHour(wp.getStartHour());
            workPlan.setUserId(wp.getUserId());
            em.persist(workPlan);
            
            return true;
        } catch(Exception e) {
            return false;
        } finally {
            if (em != null)
                em.close();
        }
    }
    
    public boolean delete(long id) {
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            WorkPlan workPlan = em.find(WorkPlan.class, new Long(id));
            em.remove(workPlan);
            
            return true;
        } catch(Exception e) {
            return false;
        } finally {
            if (em != null)
                em.close();
        }
    }
}
